//
//  LottieLoadingView.swift
//  rdb-led-banner
//
//  Created by radiobox3 on 2025/4/18.
//

import UIKit
import SnapKit

enum LoadingType{
    case LOADING
    case PROGRESS
}

class LoadingView: UIView {

    var type: LoadingType?
    var completion: (() -> Void)?
    
    convenience init(type: LoadingType){
        self.init()
        self.type = type
        switch type {
        case .LOADING:
            self.addSubview(lottieLoadingView)
            self.lottieLoadingView.snp.makeConstraints{
                $0.edges.equalToSuperview()
            }
        case .PROGRESS:
            self.addSubview(progressView)
            self.progressView.snp.makeConstraints{
                $0.edges.equalToSuperview()
            }
        }
    }
    
    func startLoading(){
        if let type = type{
            switch type {
            case .LOADING:
                lottieLoadingView.startLoading()
            case .PROGRESS:
                progressView.startLoading()
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    
    private lazy var lottieLoadingView: LottieLoadingView = {
        let lottieLoadingView = LottieLoadingView()
        lottieLoadingView.completion = { [weak self] in
            guard let self = self else { return }
            self.completion?()
        }
        return lottieLoadingView
    }()
    private lazy var progressView: ProgressLoadingView = {
        let progressView = ProgressLoadingView()
        progressView.completion = { [weak self] in
            guard let self = self else { return }
            self.completion?()
        }
        return progressView
    }()
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
